//
//  BVNavsliderView.m
//  RunLegSend1
//
//  Created by YYKJ on 2017/5/19.
//  Copyright © 2017年 YYKJ. All rights reserved.
//

#import "BVNavsliderView.h"

#define titleWidth self.size.width/3
#define titleHeight 40

#define backColor [UIColor colorWithWhite:0.300 alpha:1.000]

@implementation BVNavsliderView

-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        [self layoutUI];
    }
    return self;
}

-(void)layoutUI{
    
    NSLog(@"-----%f",self.width);
    
    UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:titleButton];
    titleButton.frame = CGRectMake(0, 0, titleWidth, 40);
    [titleButton setTitle:@"谁看过我" forState:UIControlStateNormal];
    [titleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    titleButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [titleButton addTarget:self action:@selector(scrollViewSelectToIndex:) forControlEvents:UIControlEventTouchUpInside];
    titleButton.tag = 100;
    
    UIButton *titleButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:titleButton2];
    titleButton2.frame = CGRectMake(titleWidth, 0, titleWidth , 40);
    [titleButton2 setTitle:@"我看过谁" forState:UIControlStateNormal];
    [titleButton2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    titleButton2.titleLabel.font = [UIFont systemFontOfSize:14];
    [titleButton2 addTarget:self action:@selector(scrollViewSelectToIndex:) forControlEvents:UIControlEventTouchUpInside];
    titleButton2.tag = 101;
    
    
    UIButton *titleButton3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:titleButton3];
    titleButton3.frame = CGRectMake(titleWidth*2, 0, titleWidth , 40);
    [titleButton3 setTitle:@"有好感" forState:UIControlStateNormal];
    [titleButton3 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    titleButton3.titleLabel.font = [UIFont systemFontOfSize:14];
    [titleButton3 addTarget:self action:@selector(scrollViewSelectToIndex:) forControlEvents:UIControlEventTouchUpInside];
    titleButton3.tag = 102;
    
    
    self.sliderView = [[UIView alloc]initWithFrame:CGRectMake(0, titleHeight-1, titleWidth, 1)];
    self.sliderView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.sliderView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youxi:) name:@"123" object:nil];
    
}

- (void)scrollViewSelectToIndex:(UIButton *)button{

    [self selectButton:button.tag-100];
}

- (void)selectButton:(NSInteger)index{
    
    [self.selectButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.selectButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [UIView animateWithDuration:0.1 animations:^{
        self.sliderView.frame = CGRectMake(self.width/3*index, titleHeight-1, self.width/3, 1);
        
    }];
    
    if (self.btnClick) {
        self.btnClick(index);
    }
    
}

-(void)youxi:(NSNotification *)send{
    
    BVLog(@"----传过来的值---%@",send.userInfo[@"index"]);
    [self selectButton:[send.userInfo[@"index"] intValue]];
}

@end
